import QtQuick 2.12

Item {
    id:_item
    property var titleBarShowMask: XTitleBar.SHOW_ALL

    //需要对应titleBarShowMask启用后才起效果
    property var titleBarEnableMask: XTitleBar.DISABLE_UNKNOWN
    property var spacing: 20
    property var imageWidth: 24

    readonly property alias xMenu: _imageMenu.x
    readonly property alias yMenu: _imageMenu.y


    signal sigClose()
    signal sigMin()
    signal sigMax()
    signal sigMenu()
    signal sigFreedBack()

    enum _TITLEBAR_SHOW_MASK{
        SHOW_CLOSE      =   1,
        SHOW_MIN        =   2,
        SHOW_MAX        =   4,
        SHOW_MENU       =   8,
        SHOW_FEEDBACK   =   16,
        SHOW_ALL        =   31
    }
    enum _TITLEBAE_DISABLE_MASK{
        DISABLE_UNKNOWN     =   0,
        DISABLE_CLOSE       =   1,
        DISABLE_MIN         =   2,
        DISABLE_MAX         =   4,
        DISABLE_MENU        =   8,
        DISABLE_FEEDBACK    =   16,
        DISABLE_ALL         =   31
    }

    implicitHeight: imageWidth
    implicitWidth: {
        var nWidth = 0
        if(btnIsVisible(XTitleBar.SHOW_CLOSE)){
            nWidth = imageWidth
        }

        if(btnIsVisible(XTitleBar.SHOW_MIN)){
            if(nWidth > 0){
                nWidth += spacing
            }
            nWidth += imageWidth
        }

        if(btnIsVisible(XTitleBar.SHOW_MAX)){
            if(nWidth > 0){
                nWidth += spacing
            }
            nWidth += imageWidth
        }

        if(btnIsVisible(XTitleBar.SHOW_MENU)){
            if(nWidth > 0){
                nWidth += spacing
            }
            nWidth += imageWidth
        }

        if(btnIsVisible(XTitleBar.SHOW_FEEDBACK)){
            if(nWidth > 0){
                nWidth += spacing
            }
            nWidth += imageWidth
        }
        return nWidth
    }

    function btnIsVisible(mask){
        return mask === (mask&titleBarShowMask)
    }

    function btnIsEnable(mask){
        return mask !== (mask&titleBarEnableMask)
    }

    XImage{
        id:_imageClose
        anchors.right: parent.right
        y:0

        normalImage: "qrc:/res/ico_close_normal.png"
        disableImage: "qrc:/res/ico_close_disable.png"
        pressImage: "qrc:/res/ico_close_press.png"
        hoverImage: "qrc:/res/ico_close_hover.png"

        width:imageWidth
        height: width
        enabled:btnIsEnable(XTitleBar.DISABLE_CLOSE)
        visible: btnIsVisible(XTitleBar.SHOW_CLOSE)
        onClicked:{
            console.log("XTitleBar sigClose")
            _item.sigClose()
        }
    }

    XImage{
        id:_imageMax
        anchors.right: parent.right
        anchors.rightMargin: _imageClose.visible?(imageWidth+spacing):0
        y:0

        normalImage: "qrc:/res/ico_quanping_normal.png"
        disableImage: "qrc:/res/ico_quanping_disable.png"
        pressImage: "qrc:/res/ico_quanping_press.png"
        hoverImage: "qrc:/res/ico_quanping_hover.png"

        width:imageWidth
        height: width
        enabled:btnIsEnable(XTitleBar.DISABLE_MAX)
        visible: btnIsVisible(XTitleBar.SHOW_MAX)
        onClicked:{
            console.log("XTitleBar sigMax")
            _item.sigMax()
            var isIMaxed = (window.visibility === 4 || window.visibility === 5) ? true: false
            _imageMax.updateImg(isIMaxed)
        }

        function updateImg(isMaxed){
            if(isMaxed){
                normalImage = "qrc:/res/ico_tuichuquanping_normal.png"
                disableImage = "qrc:/res/ico_tuichuquanping_disable.png"
                pressImage = "qrc:/res/ico_tuichuquanping_press.png"
                hoverImage = "qrc:/res/ico_tuichuquanping_hover.png"
            }else{
                normalImage = "qrc:/res/ico_quanping_normal.png"
                disableImage = "qrc:/res/ico_quanping_disable.png"
                pressImage = "qrc:/res/ico_quanping_press.png"
                hoverImage = "qrc:/res/ico_quanping_hover.png"
            }
        }
    }

    XImage{
        id:_imageMin
        anchors.right: parent.right
        anchors.rightMargin: {
            var tmp = _imageClose.visible?(imageWidth+spacing):0
            tmp += _imageMax.visible?(imageWidth+spacing):0
            return tmp
        }
        y:0

        normalImage: "qrc:/res/ico_small_normal.png"
        disableImage: "qrc:/res/ico_small_normal.png"
        pressImage: "qrc:/res/ico_small_press.png"
        hoverImage: "qrc:/res/ico_small_hover.png"

        width:imageWidth
        height: width
        enabled:btnIsEnable(XTitleBar.DISABLE_MIN)
        visible: btnIsVisible(XTitleBar.SHOW_MIN)
        onClicked:{
            console.log("XTitleBar sigMin")
            _item.sigMin()
        }
    }

    XImage{
        id:_imageMenu
        anchors.right: parent.right
        anchors.rightMargin: {
            var tmp = _imageClose.visible?(imageWidth+spacing):0
            tmp += _imageMax.visible?(imageWidth+spacing):0
            tmp += _imageMin.visible?(imageWidth+spacing):0
            return tmp
        }
        y:0

        normalImage: "qrc:/res/ico_menu_normal.png"
        disableImage: "qrc:/res/ico_menu_disable.png"
        pressImage: "qrc:/res/ico_menu_press.png"
        hoverImage: "qrc:/res/ico_menu_hover.png"

        width:imageWidth
        height: width
        enabled:btnIsEnable(XTitleBar.DISABLE_MENU)
        visible: btnIsVisible(XTitleBar.SHOW_MENU)
        onClicked:{
            console.log("XTitleBar sigMenu")
            _item.sigMenu()
        }
    }

    XImage{
        id:_imageFreedBack
        anchors.right: parent.right
        anchors.rightMargin: {
            var tmp = _imageClose.visible?(imageWidth+spacing):0
            tmp += _imageMax.visible?(imageWidth+spacing):0
            tmp += _imageMin.visible?(imageWidth+spacing):0
            tmp += _imageMenu.visible?(imageWidth+spacing):0
            return tmp
        }
        y:0

        normalImage: "qrc:/res/ico_feedback_normal.png"
        disableImage: "qrc:/res/ico_feedback_disable.png"
        pressImage: "qrc:/res/ico_feedback_press.png"
        hoverImage: "qrc:/res/ico_feedback_hover.png"

        width:imageWidth
        height: width
        enabled:btnIsEnable(XTitleBar.DISABLE_FEEDBACK)
        visible: btnIsVisible(XTitleBar.SHOW_FEEDBACK)
        onClicked:{
            console.log("XTitleBar sigFreedBack")
            _item.sigFreedBack()
        }
    }
}
